package com.qjdchina.sku.biz.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qjdchina.commons.result.Result;
import com.qjdchina.sku.biz.SkuCompanyManager;
import com.qjdchina.sku.converter.SkuCompanyConverter;
import com.qjdchina.sku.dao.mapper.SkuCompanyMapper;
import com.qjdchina.sku.model.SkuCompany;
import com.qjdchina.sku.model.vo.SkuCompanyVO;
import com.qjdchina.sku.service.enums.SkuResultCode;


/**
 * 公司
 * @author shenpingfeng
 * 2016年12月1日
 */
@Service
public class SkuCompanyManagerImpl implements SkuCompanyManager{

    public static final Logger LOGGER = LoggerFactory.getLogger(SkuCompanyManagerImpl.class);

    @Autowired
    private SkuCompanyMapper skuCompanyMapper;

    /**
	 * 根据companyId查询 
	 * shenpingfeng
	 * 2016年12月1日
	 * @param companyId
	 * @return
	 */
    @Override
    public Result<SkuCompanyVO> findSkuCompanyByCompanyId(Integer companyId) {
    	if(companyId == null){
    		return new Result<SkuCompanyVO>(SkuResultCode.PARAMETER_NOT_EMPTY);
    	}
    	SkuCompany skuCompany = skuCompanyMapper.selectByPrimaryKey(companyId);
    	if(skuCompany == null){
    		return new Result<SkuCompanyVO>(SkuResultCode.DATA_NOT_EXIST);
    	}
    	SkuCompanyVO skuCompanyVO = SkuCompanyConverter.toSkuCompanyVO(skuCompany);
        return new Result<SkuCompanyVO>(skuCompanyVO);
    }
    
    /**
	 * 更新
	 * shenpingfeng
	 * 2017年1月5日
	 * @param skuCompany
	 * @return
	 */
    @Override
    public Result<Integer> updateSkuCompany(SkuCompanyVO skuCompanyVO) {
    	if(skuCompanyVO == null){
    		return new Result<Integer>(SkuResultCode.PARAMETER_NOT_EMPTY);
    	}
    	SkuCompany skuCompany = SkuCompanyConverter.toSkuCompany(skuCompanyVO);
    	int row = skuCompanyMapper.updateByPrimaryKeySelective(skuCompany);
    	if(row == 0){
    		return new Result<Integer>(SkuResultCode.UPDATE_FAIL);
    	}
        return new Result<Integer>(row);
    }
    
	/**
	 * 创建
	 * shenpingfeng
	 * 2017年1月12日
	 * @param skuCompanyVO
	 * @return
	 */
    @Override
    public Result<Integer> create(SkuCompanyVO skuCompanyVO) {
    	if(skuCompanyVO == null){
    		return new Result<Integer>(SkuResultCode.PARAMETER_NOT_EMPTY);
    	}
    	SkuCompany skuCompany = SkuCompanyConverter.toSkuCompany(skuCompanyVO);
    	int row = skuCompanyMapper.insertSelective(skuCompany);
    	if(row == 0){
    		return new Result<Integer>(SkuResultCode.ADD_FAIL);
    	}
        return new Result<Integer>(row);
    }
    
	/**
	 * 根据公司名称查询 
	 * shenpingfeng
	 * 2017年1月12日
	 * @param companyName
	 * @return
	 */
    @Override
    public Result<SkuCompanyVO> getCompanyByCompanyName(String companyName) {
    	if(StringUtils.isBlank(companyName)){
    		return new Result<SkuCompanyVO>(SkuResultCode.PARAMETER_NOT_EMPTY);
    	}
    	SkuCompany skuCompany = skuCompanyMapper.selectByCompanyName(companyName);
    	if(skuCompany == null){
    		return new Result<SkuCompanyVO>(SkuResultCode.DATA_NOT_EXIST);
    	}
    	SkuCompanyVO skuCompanyVO = SkuCompanyConverter.toSkuCompanyVO(skuCompany);
        return new Result<SkuCompanyVO>(skuCompanyVO);
    }
    
    /**
	 * 根据所有公司
	 * shenpingfeng
	 * 2017年1月13日
	 * @return
	 */
    @Override
    public Result<List<SkuCompany>> getAllCompany() {
    	List<SkuCompany> skuCompanies = skuCompanyMapper.selectAllCompany();
        return new Result<List<SkuCompany>>(skuCompanies);
    }

}
